
var BattleManager = {

	enemyTanksData : null,	// 敌方坦克数据

	data : null,	// 战斗log数据
	maxFrame : 0,
	curFrame : 0,


	initScene : function () {
		if (!this.scene) {
			// UIBattle.ccb 中的某一个Node的引用, 用来放置战斗场景
			var layer = getUIBattle().uiBattleScene;
			var upLayer = getUIBattle().uiBattleSceneUpLayer;

			cc.log('============:uiBattleSceneUpLayer:'+getUIBattle().uiBattleSceneUpLayer)
			BattleLayer.init(layer, upLayer);
		}

		/*
		BattleLayer.createUnit(0, 99, 0, 0, 0);

		BattleLayer.createUnit(1, 99, 1, 0, 0);

        BattleLayer.unitAttack(null);
		*/

    	// 粒子系统
    	/*
    	var p = FXManager.crateFXParticleSystem('fx/green_fire.plist');
    	this.scene.addChild(p);
    	FXManager.moveFX(p, 10, {x:480, y:160});
		*/

		// FXManager.launchParticleSystem('fx/green_fire.plist', this.scene, 2, {x:10, y:160}, {x:480, y:160});
	},
    
    
    // 进入PVE
    enterBattlePve : function (missionId) {
    	
    	cc.log("enterBattlePve: missionId:"+missionId);
    	// 背景
		BattleLayer.setBg('res/battle/map/2.png');

		//* 读取关卡数据
		mapId = TB_mission[missionId].data_id;
		cc.log('mapId:'+mapId)
		mapData = TB_map[mapId];

		tmpId = 0;
		tanksData = [];
		for each(var d in mapData.tanks) {
			tmpId += 1
			hp = TB_tank[d.type].hp;	// 读取坦克表格

			tk = {
				'id' : tmpId,
				'type' : d.type,
				'pos' : [d.x, d.y],
				'hp' : hp,
				'mhp' : hp,
			};
			tanksData.push(tk);
		}
		this.enemyTanksData = tanksData;
		//* 设置地图数据 障碍物 阻挡块

		// 初始化血量
		getUIBattle().setHp(helper.SIDE_UP, 1);
		getUIBattle().setHp(helper.SIDE_DOWN, 1);

		// 打开部署界面
    	getUIBattle().showDeployUI(true);

		// 部署 - 敌方部队
    	g1 = new Garage(helper.CELLS_UP_BASE);
    	getUIBattle().getDeployLayer().addChild(g1);
    	g1.setTanksData(tanksData, helper.SIDE_UP);
    	g1.setDeployEnabled(false);

		// 部署 - 我方部队
    	g2 = new Garage(helper.CELLS_DOWN_BASE);
    	getUIBattle().getDeployLayer().addChild(g2);
    	g2.setTanksData(DataManager.playerTanks, helper.SIDE_DOWN);

		// 之后就开始部署了
    },
    

    // 部署结束(from UIBattle)
    onDeployOver : function () {
    	cc.log('onDeployOver');
    	// 关闭部署界面
    	getUIBattle().showDeployUI(false);
    	getUIBattle().getDeployLayer().removeAllChildren();	// 删除掉部署用的Garage

		// 初始化战场
		// 创建敌方部队 *
		var tanks = this.enemyTanksData;
		for each(var d in tanks) {
			//cc.log('创建敌方部队 ----')
			//cc.dump(d)
			var isAI = true;
            BattleLayer.createUnit(d.id+900000, d.type, helper.SIDE_UP, d.pos[0], d.pos[1], d.hp, d.mhp, isAI);
        }

		// 创建我方部队
		var tanks = DataManager.playerTanks;
		for each(var d in tanks) {
            BattleLayer.createUnit(d.id, d.type, helper.SIDE_DOWN, d.pos[0], d.pos[1], d.hp, d.mhp);
        }
		
    	// 双方入场之后开始战斗
    	BattleLayer.onBattleStart();
    },

	onBattleEnd : function () {

		// 清理战场
		BattleLayer.onBattleEnd();

		// 场景切换
		getUIMain().onExitBattleScene();

		//* 发送结果, 获得结果, 出现结算画面

		/*
			// 结束循环
			this.stopUpdate();

			// 清理资源
			this.scene = null;
			
			//* 黑屏,发送请求玩家主界面数据,并等待
            gnet.sendHttp("get_main_data", null, function(data) {
                if(data['result']=='ok') {
	                DataManager.setData(data['data']);
	                // 切换场景
	                UIManager.changeSceneMain();
	            }
	        }); */
	},

	update : function () {
	},


	// update控制 ------------------
	startUpdate : function(interval) {
    	cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(
    		this, this.update, interval, cc.REPEAT_FOREVER);
	},

	pauseUpdate : function() {
    	cc.Director.getInstance().getScheduler().pauseTarget(this);
	},

	resumeUpdate : function() {
    	cc.Director.getInstance().getScheduler().resumeTarget(this);
	},

	stopUpdate : function() {
    	cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(this, this.update);
	},
};

